package com.cl.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.cl.pojo.Department;
import com.cl.pojo.Post;
import com.cl.pojo.User;
import com.cl.pojo.UserInfo;
import com.cl.service.impl.DepartmentServiceImpl;
import com.cl.service.impl.PostServiceImpl;
import com.cl.service.impl.UserInfoServiceImpl;
import com.cl.utils.MegUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class UserInfoController {
    @Autowired
    private UserInfoServiceImpl userInfoService;
    @Autowired
    private DepartmentServiceImpl departmentService;
    @Autowired
    private PostServiceImpl postService;


    @ResponseBody
    @RequestMapping("/queryall")
    //page 当前页数
    public String queryall(@RequestParam("page") int page,@RequestParam("limit") int limit){
        List<Map<String, Object>> maps = userInfoService.queryAllUserInfo();
        List<Map<String, Object>> maps1 = new ArrayList<>();
        int size = maps.size();
        for (int i = ((page-1)*limit); i < limit*page; i++) {
            maps1.add(maps.get(i));
            if (i==size-1){
                break;
            }
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("count",size);
        map.put("code",0);
        map.put("msg","");
        map.put("data",maps1);
        return JSON.toJSONStringWithDateFormat(map,"yyyy-MM-hh", SerializerFeature.WriteDateUseDateFormat);
    }
    @ResponseBody
    @RequestMapping("/querybyuid")
    public String queryByUid(HttpServletRequest req){
        HttpSession session = req.getSession();
        User login = (User) session.getAttribute("login");
        int uid = login.getUid();
        List<Map<String, Object>> maps = userInfoService.queryByUid(uid);
        return JSON.toJSONStringWithDateFormat(maps,"yyyy-MM-hh", SerializerFeature.WriteDateUseDateFormat);
    }

    @ResponseBody
    @RequestMapping("/insertinfo")
    public String insertInfo(@RequestParam("s") String jsonString ,HttpServletRequest req){
        HttpSession session = req.getSession();
        User login = (User) session.getAttribute("login");
        int uid = login.getUid();
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        //取值
        String department = jsonObject.getString("departmentId");
        //赋值
        Department department1 = departmentService.queryByDepartmentname(department);
        int departmentId = department1.getDepartmentId();
        jsonObject.put("departmentId",departmentId);

        String post = jsonObject.getString("postId");
        Post post1 = postService.queryByPostName(post);
        int postid = post1.getPostid();
        jsonObject.put("postId",postid);


        String sex = jsonObject.getString("sex");
        if (sex.equals("男")){
            jsonObject.put("sex",0);
        }else {
            jsonObject.put("sex",1);
        }

        //将json对象转为实体类
        JSONObject jsonObject1 = JSON.parseObject(jsonObject.toString());
        UserInfo userInfo = JSON.toJavaObject(jsonObject1, UserInfo.class);
        userInfo.setUid(uid);

        int i = userInfoService.updateInfo(userInfo);
        if (i==1){
            return JSON.toJSONString(MegUtil.success("修改成功"));
        }else {
            return JSON.toJSONString(MegUtil.fail("修改失败"));
        }
    }
}
//{"username":"121","email":"12","phone":"21","university":"21","entrytime":"2020-10-20","department":"人力资源部","post":"部门经理","sex":"男"}
//UserInfo{uid=0, Name='null', sex=0, email='2', phone=3, university='4', quit=0, departmentId=0, postId=0}